package com.li.sort;

import java.util.Arrays;

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {-2, -4, 0, -5, 8, 6};
        quick(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));
    }

    public static void quick(int[] arr, int left, int right) {
        int l = left;
        int r = right;
        int privot = arr[(left + right) / 2];//中轴值

        int temp = 0;//临时变量，交换使用
        //比中小的放在左边
        while (l < r) {

            //在pei左边一直找，找大于等于pri的值
            while (arr[l] < privot) {
                l += 1;
            }
            while (arr[r] > privot) {
                r -= 1;
            }
            if (l >= r) {
                break;
            }
            //交换
            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            if (arr[l] == privot) {
                r -= 1;
            }
            if (arr[r] == privot) {
                l -= 1;
            }
        }
        if (l == r) {
            l = l + 1;
            r = r - 1;
        }
        if (left < r) {
            quick(arr, left, r);
        }
        if (right > l) {
            quick(arr, l, right);
        }


    }

}
